﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
        }

        #region Form_Load
        private void Home_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            SqlCommand cmdDatabase = new SqlCommand("Select month(Check_in_Date) as [Month], count(Check_in_Date) as [Number of Bookings] from Booking Group by month(Check_in_Date)", connection);           
            SqlDataReader dr;
            try
            {
                connection.Open();
                dr = cmdDatabase.ExecuteReader();         
                while (dr.Read())
                {
                    this.chart2.Series["Month"].Points.AddXY(dr["Month"], dr["Number of Bookings"]);
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            SqlCommand cmdDatabase2 = new SqlCommand("SELECT Room_Status as [Room Status] , Round((COUNT(Room_Status)*100.0/(Select count (*) from Room)),2) AS [Number of Rooms] FROM Room GROUP BY Room_Status ORDER BY Room_Status DESC", connection);
            SqlDataReader dr2;
            try
            {
                connection.Open();
                dr2 = cmdDatabase2.ExecuteReader();
                while (dr2.Read())
                {
                    this.chart1.Series["Room Status"].Points.AddXY(dr2["Room Status"], dr2["Number of Rooms"]);
                }
                connection.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }

            SqlCommand cmdDatabase3 = new SqlCommand("Select month(bk.Check_out_Date) as [Month], Sum(i.Total_Payment) as [Total Revenue]  from Invoice i, Booking bk where i.Booking_ID=bk.Booking_ID Group by month(bk.Check_out_Date)", connection);
            SqlDataReader dr3;
            try
            {
                connection.Open();
                dr3 = cmdDatabase3.ExecuteReader();
                while (dr3.Read())
                {
                    this.chart3.Series["Total Revenue"].Points.AddXY(dr3["Month"], dr3["Total Revenue"]);
                }
                connection.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion Form_Load

    }
}
